﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.Security.Principal;
using System.Net;
using System.ServiceModel.Channels;

namespace CMISTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRepositoryService_Click(object sender, EventArgs e)
        {
            CMISServices.RepositoryServicePortClient rspc = new CMISServices.RepositoryServicePortClient();
            rspc.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
            rspc.ClientCredentials.Windows.ClientCredential = new NetworkCredential("administrator", "K00fax", "moss2010-hn");

            CMISServices.cmisRepositoryEntryType[] repos = rspc.getRepositories(null);
        }

        private void btnRepositoryService2_Click(object sender, EventArgs e)
        {
            EndpointAddress endpointAddress = new EndpointAddress("https://moss2010-hn/_vti_bin/cmis/soap/RepositoryService.svc/ntlm");

            BasicHttpBinding binding = new BasicHttpBinding();
            binding.MessageEncoding = WSMessageEncoding.Mtom;
            //binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Mode = BasicHttpSecurityMode.Transport;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

            //CustomBinding binding;
            
            //List<BindingElement> elements = new List<BindingElement>();

            //SecurityBindingElement securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
            //securityElement.SecurityHeaderLayout = SecurityHeaderLayout.LaxTimestampFirst;
            //elements.Add(securityElement);

            //MtomMessageEncodingBindingElement mtomElement = new MtomMessageEncodingBindingElement();
            //mtomElement.MessageVersion = MessageVersion.Soap11;
            //elements.Add(mtomElement);

            //HttpsTransportBindingElement transportElement = new HttpsTransportBindingElement();
            //transportElement.TransferMode = TransferMode.Streamed;
            //elements.Add(transportElement);

            //binding = new CustomBinding(elements);

            CMISServices.RepositoryServicePortClient rspc = new CMISServices.RepositoryServicePortClient(binding, endpointAddress);

            rspc.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
            rspc.ClientCredentials.Windows.ClientCredential = new NetworkCredential("administrator", "K00fax", "");
            //rspc.ClientCredentials.UserName.UserName = "moss2010-hn\administrator";
            //rspc.ClientCredentials.UserName.Password = "K00fax";

            CMISServices.cmisRepositoryEntryType[] repos = rspc.getRepositories(null);
            CMISServices.cmisRepositoryInfoType info = rspc.getRepositoryInfo("7b4248b6-0c30-41c6-8142-567a9f4f58ab", null);
        }

        private void btnGetRepositoriesNuxeo_Click(object sender, EventArgs e)
        {
            EndpointAddress endpointAddress = new EndpointAddress("http://cmis.demo.nuxeo.org:80/nuxeo/webservices/cmis/RepositoryService");
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.MessageEncoding = WSMessageEncoding.Mtom;
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            CMISServices.RepositoryServicePortClient rspc = new CMISServices.RepositoryServicePortClient(binding, endpointAddress);

            //rspc.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
            //rspc.ClientCredentials.Windows.ClientCredential = new NetworkCredential("Administrator", "Administrator", "");
            rspc.ClientCredentials.UserName.UserName = "Administrator";
            rspc.ClientCredentials.UserName.Password = "K00fax";

            CMISServices.cmisRepositoryEntryType[] repos = rspc.getRepositories(null);
        }
    }
}
